package com.zdxlz.fcmp.upms.api.service.impl;

import cn.hutool.core.bean.BeanUtil;
import cn.hutool.core.util.ObjectUtil;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.zdxlz.fcmp.common.core.exception.BizException;
import com.zdxlz.fcmp.upms.api.dto.SystemLogReqDto;
import com.zdxlz.fcmp.upms.api.entity.SystemExceptionLog;
import com.zdxlz.fcmp.upms.api.entity.SystemLoginLog;
import com.zdxlz.fcmp.upms.api.entity.SystemOperLog;
import com.zdxlz.fcmp.upms.api.service.ISystemExceptionLogService;
import com.zdxlz.fcmp.upms.api.service.ISystemLoginLogService;
import com.zdxlz.fcmp.upms.api.service.ISystemOperLogService;
import com.zdxlz.fcmp.upms.api.vo.SystemExceptionLogVo;
import com.zdxlz.fcmp.upms.api.vo.SystemLoginLogVo;
import com.zdxlz.fcmp.upms.api.vo.SystemOperLogVo;
import jakarta.annotation.Resource;
import org.springframework.stereotype.Service;

import java.util.List;
import java.util.stream.Collectors;

import static com.zdxlz.fcmp.common.core.constant.enums.ResCode.LOG_PARAMETER_MISSING;

/**
 * @Description：
 * @Author：aman
 * @Data 2025/3/18 14:30
 */
@Service
public abstract class AbstractSystemLogService<T> {

    @Resource
    private ISystemLoginLogService loginLogService;

    @Resource
    private ISystemOperLogService operLogService;

    @Resource
    private ISystemExceptionLogService exceptionLogService;

    /**
     * @Description：根据日志类型查询日志数据
     * @Auther：aman
     * @Date：2025/3/18 14:54
     */
    public IPage<T> selectSystemLog(Page page, SystemLogReqDto dto) {
        if (ObjectUtil.isEmpty(dto) || ObjectUtil.isEmpty(dto.logType()) || ObjectUtil.isEmpty(dto.tenantId()))
            throw new BizException(LOG_PARAMETER_MISSING.getCode(), LOG_PARAMETER_MISSING.getReason());
        Page<T> pageResult = switch (dto.logType()) {
            case LOGIN -> loginLogService.selectSystemLoginLog(page, dto);
            case OPERATION -> operLogService.selectSystemOperLog(page, dto);
            case EXCEPTION -> exceptionLogService.selectSystemExceptionLog(page, dto);
        };
        List<T> systemLogList = pageResult.getRecords().stream().map(this::convertToT).collect(Collectors.toList());
        IPage<T> iPage = new Page<>(pageResult.getCurrent(), pageResult.getSize(), pageResult.getTotal());
        iPage.setRecords(systemLogList);
        return iPage;
    }

    /**
     * @description：系统日志实体装换为系统日志视图
     * @author：aman
     * @date：2025/2/14 16:27
     */
    private T convertToT(T t) {
        if (t instanceof SystemLoginLog) {
            SystemLoginLogVo loginLogVo = new SystemLoginLogVo();
            BeanUtil.copyProperties(t, loginLogVo);
            return (T) loginLogVo;
        } else if (t instanceof SystemOperLog) {
            SystemOperLogVo operLogVo = new SystemOperLogVo();
            BeanUtil.copyProperties(t, operLogVo);
            return (T) operLogVo;
        } else if (t instanceof SystemExceptionLog) {
            SystemExceptionLogVo exceptionLogVo = new SystemExceptionLogVo();
            BeanUtil.copyProperties(t, exceptionLogVo);
            return (T) exceptionLogVo;
        }
        return null;
    }
}
